﻿@*
    For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
*@

@{
    //Layout = "~/Views/Shared/_FormLayout.cshtml";
    Layout = "~/Views/Shared/__ComponentLayout.cshtml";
    string editId = ViewData.Count > 0 ? ViewData["0"].ToString() : "null";
}
@using ST.Torch.WebApi.Web.Core;
@inherits UtilRazorPage<object>
<div style="padding: 16px;"></div>

<form style="padding: 16px;padding-right:30px"  class="layui-form" action=""  lay-filter="edit-data">
      <input type="text" name="Id"  placeholder="" autocomplete="off" class="layui-input" style="display:none">
    <div class="layui-form-item">
        <label class="layui-form-label">@L("上级菜单")</label>
        <div class="layui-input-block">
            <ul id="ParentModule" class="dtree" data-id="null"></ul>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">@L("名称")</label>
        <div class="layui-input-block">
            <input type="text" name="Title" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">@L("路径")</label>
        <div class="layui-input-block">
            <input type="text" name="Href" required lay-verify="required" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">@L("权限标识")</label>
        <div class="layui-input-block">
            <input type="text" name="PermissionId" placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">@L("排序")</label>
        <div class="layui-input-block">
            <input type="number" lay-affix="number" value="0" name="Sort"  step="1" class="layui-input">
          
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label">@L("图标")</label>
        <div class="layui-input-block">
            <input type="text" name="Icon"  placeholder="" autocomplete="off" class="layui-input">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">@L("打开方式")</label>
        <div class="layui-input-block">
            <input type="radio" name="OpenType" value="_component" title="@L("_component")" checked>
            <input type="radio" name="OpenType" value="_blank" title="@L("_blank")">
            <input type="radio" name="OpenType" value="_iframe" title="@L("嵌套网页")">
            <input type="radio" name="OpenType" value="_layer" title="@L("弹窗网页")">
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">@L("类型")</label>
        <div class="layui-input-block">
            <input type="radio" name="Type" value="0" title="@L("菜单")" checked>
            <input type="radio" name="Type" value="1" title="@L("页面")">
            <input type="radio" name="Type" value="2" title="@L("按钮")">
        </div>
    </div>
    

   
    <div class="layui-form-item">
        <label class="layui-form-label">@L("是否隐藏")</label>
        <div class="layui-input-block">
            <input type="checkbox" name="IsHide" lay-skin="switch" value="true" lay-text="@L("隐藏")|@L("开启")">
        </div>
    </div>
    
    
    <div class="layui-form-item FormSubmit" style="text-align:right">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="formDemo">@L("立即提交")</button>
            <button type="reset" class="layui-btn layui-btn-primary addbut">@L("重置")</button>
            <button type="button" lay-filter="Close" class="layui-btn layui-btn-primary updatebut">@L("关闭")</button>
        </div>
    </div>
</form>
 @section footer
{
<script>
    //Demo
    layui.use(['form', 'toast', 'dtree'], function () {
        var form = layui.form;
        var toast = layui.toast;
        var dtree = layui.dtree;
        console.log("*--*");
        //var editId='@GetValue("Id","null")';
        var editId = '@editId';
        var IsUpdate = editId != "null" ? true : false;

        var init_select = function (selectInitVal) {
            dtree.render({
                elem: "#ParentModule",
                initLevel: "1",
                method: 'get',
                url: "/api/SYSMenu/GetSelectTreeMenu",
                select: true,
                selectInitVal: `${selectInitVal}`

            });
        }
        $(".addbut").hide();
        $(".updatebut").hide();
        if (IsUpdate) {
            $(".updatebut").show();
            $("#AccountEle").attr("disabled", "disabled")
            Query();
        } else {
            $(".addbut").show();
            //默认初始值
            // var tempData = {
            //     IsEnable: true
            // }
            // form.val("edit-data", tempData);
            init_select(GuidEmpty)
            form.render();
        }
        //监听按钮
        $(".layui-form button").click(function () {
            var id = $(this).attr('lay-filter');
            //alert(id);
            switch (id) {
                case "Close": closeOpen(); break;
            }
        });

            var buttonEle = null;
        //监听提交
        form.on('submit(formDemo)', function (data) {
            console.log(data.field)
            buttonEle = button.load({ elem: this });

            data.field.ParentId = data.field.ParentModule_select_nodeId || GuidEmpty;
            //layer.msg(JSON.stringify(data.field));
            if (IsUpdate) {
                Update(data);
            } else {
                Add(data);
            }

            return false;
        });

        function Query() {
            console.log("--Query--")


            var formData = {
                Id: editId
            };
            $.ajax({
                type: 'POST',
                url: '/api/SYSMenu/Single',//发送请求
                data: formData,
                success: function (result) {
                    console.log(result)
                    if (result.Succeeded) {
                        form.val("edit-data", result.Data);
                        init_select(result.Data.ParentId);


                    } else {
                        toast.error({
                            message: '@L("查询失败")',
                        });

                    }
                }
            });

        }

        function Add(data) {
            $.ajax({
                type: 'POST',
                url: '/api/SYSMenu/Insert',//发送请求
                data: data.field,
                success: function (result) {
                    console.log(result)
                    buttonEle.stop();
                    if (result.Succeeded) {

                        toast.success({
                            message: '@L("添加成功")',
                        });
                        closeOpen();
                    } else {
                        var msgError = '@L("添加失败")';
                        if (result.Errors != "") {
                            msgError = result.Errors
                        }
                        toast.error({
                            message: msgError,
                        });
                    }
                }, error: function (err) {
                    buttonEle.stop();
                }
            });

        }
        function Update(data) {
            $.ajax({
                type: 'POST',
                url: '/api/SYSMenu/Update',//发送请求
                data: data.field,
                success: function (result) {
                    buttonEle.stop();
                    console.log(result)
                    if (result.Succeeded) {
                        toast.success({
                            message: '@L("修改成功")',
                        });
                        closeOpen();
                    } else {
                        var msgError = '@L("修改失败")';
                        if (result.Errors != "") {
                            msgError = result.Errors
                        }
                        toast.error({
                            message: msgError,
                        });
                    }
                }, error: function (err) {
                    buttonEle.stop();
                }
            });

        }
        function closeOpen() {

            parent.layer.close(G_LayuiOpenTypeIndex);
            return true;
        }

    });
</script>
}